package temp

// todo: 迁移到array 目录中
// https://leetcode-cn.com/problems/grumpy-bookstore-owner/
// 算法: 滑动窗口，直接确认每一种情况下 顾客的满意数量，窗口不断向右移动
// 时间复杂度: O(n)
func MaxSatisfied(customers []int, grumpy []int, X int) int {
	start, end := 0, X-1
	totalMinutes := len(customers)
	if end >= totalMinutes {
		end = totalMinutes - 1
	}
	// 设置初始状态下的用户满意度
	currentSatisfied := 0
	for index := 0; index < totalMinutes; index++ {
		if (index >= start && index <= end) || grumpy[index] == 0 {
			currentSatisfied += customers[index]
		}
	}
	// 滑动窗口
	maxSatisfied := currentSatisfied
	for {
		lastStart := start
		start++
		end++
		if end >= totalMinutes {
			break
		}

		if grumpy[lastStart] == 1 {
			currentSatisfied -= customers[lastStart]
		}
		// 注意要避免重复添加
		if grumpy[end] == 1 {
			currentSatisfied += customers[end]
		}

		if maxSatisfied < currentSatisfied {
			maxSatisfied = currentSatisfied
		}
	}
	return maxSatisfied
}
